// pages/my/my.js
import {
  request
} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    user: {},
    id: 1,
    revenue: 0,
    openid: '',
  },
  onShow: function () {
    this.getData();
  },
  //换头像
  changeImg() {
    let {
      userInfo
    } = this.data;
    console.log(userInfo);
    wx.chooseImage({
      count: 1,
      type: 'image',
      success: (result) => {
        console.log(result.tempFiles[0]);
        // console.log(wx.getFileSystemManager().readFileSync(result.tempFiles[0].path,'base64'));
        request({
          url: 'user/updUserIcon',
          method: 'POST',
          data: {
            IconPath: wx.getFileSystemManager().readFileSync(result.tempFiles[0].path, 'base64'),
            open_id: this.data.user.open_id
          },
        }).then((result) => {
          console.log(result);
          this.setData({
            user: result.data.data
          });
          wx.setStorageSync('user', result.data.data);
        });
      }
    })
  },
  editName() {
    wx.navigateTo({
      url: '../selfManage/selfManage',
    })
  },
  onPullDownRefresh() {
    this.onRefresh();
  },
  onRefresh() {
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '刷新中...',
    })
    //在当前页面显示导航条加载动画
    if(this.data.user==''||Object.keys(this.data.user).length==0){
      setTimeout(() => {
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }, 1000);
    }else{
      let openid = wx.getStorageSync('openid');
      request({
        url: 'user/RefreshUserInfo',
        data: {
          open_id: openid
        }
      }).then(result => {
        console.log(result);
        setTimeout(() => {
          //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
        }, 1000);
        wx.setStorageSync('user', result.data.data);
        if (result.data.data.user_phone == null || result.data.data.user_phone == '') {
          console.log(1111);
          wx.navigateTo({
            url: '../../pages/phoneNum/phoneNum'
          })
        } else {
          wx.reLaunch({
            url: '../../pages/my/my',
          });
        }
      })
    }
   
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
   
 
  },

  getData() {
    let user = wx.getStorageSync('user');
    let userInfo = wx.getStorageSync('userInfo');
    //JSON.parse()里只能是字符串，将值存到storage之后不会改变值得类型，此时值就不是字符串
    // let userInfo = typeof wx.getStorageSync('userInfo') === 'string' ? JSON.parse(wx.getStorageSync('userInfo')) : wx.getStorageSync('userInfo');
    // let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    this.setData({
      userInfo,
      user
    });

  },
  goClick(e) {
    console.log(e);
    if (Object.keys(this.data.user).length != 0) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
    }
  },
  //登录
  getUserInfo(e) {
    console.log("用户登录...");
    let that = this;
 
      wx.login({
        //获取code
        success: function (res) {
          wx.setStorageSync('userInfo', e.detail.rawData);
          var code = res.code; //返回code
          console.log(code);
          var appId = 'wx7a9b0cafff1a532e';
          var secret = 'f36f0e13e423d8a8930175bba4bb3e2f';
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
            data: {},
            header: {
              'content-type': 'json'
            },
            success: function (res) {
              console.log(res);
              let userInfo = typeof wx.getStorageSync('userInfo') === 'string' ? JSON.parse(wx.getStorageSync('userInfo')) : wx.getStorageSync('userInfo');
              console.log(userInfo.nickName);
              console.log(userInfo.avatarUrl);
              var openid = res.data.openid //返回openid
              wx.setStorageSync('openid', openid);
              console.log('openid为' + openid);
              that.setData({
                userInfo,
                openid
              })
              request({
                url: 'user/userinfo',
                data: {
                  user_name:userInfo.nickName,
                  user_icon:userInfo.avatarUrl,
                  open_id: that.data.openid
                }
              }).then((result) => {
                console.log(result);
                wx.setStorageSync('user', result.data.data);
                if (result.data.data.user_phone == null || result.data.data.user_phone == '') {
                  console.log(1111);
                  wx.navigateTo({
                    url: '../../pages/phoneNum/phoneNum'
                  })
                } else {
                  wx.reLaunch({
                    url: '../../pages/my/my',
                  });
                }
              })
            }
          })
        }
      })
    



  },

})